package cn.icanci.loopstack.bic.biz.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.text.StringSubstitutor;
import org.springframework.core.io.ClassPathResource;

import com.google.common.collect.Maps;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.bic.common.enums.NodeDataModeEnum;
import cn.icanci.loopstack.bic.common.enums.NodeVueTypeEnum;
import cn.icanci.loopstack.bic.graph.node.UnitViewNode;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 09:33
 */
public class TemplateFileUtils {
    private static final String DEFAULT_NODE_STYLE_TEMPLATE = "style/default-node-style-template.json";
    private static final String DEFAULT_FREE_NODE_STYLE_TEMPLATE = "style/default-free-node-style-template.json";
    private static final String DEFAULT_NODE_EDGE_TEMPLATE  = "style/default-edge-style-template.json";
    private static final String DEFAULT_NODE_VUE_TEMPLATE   = "vue/default-node-vue-template.vue";
    private static final String DEFAULT_NODE_VUE            = "vue/default-node-vue.vue";

    /**
     * queryMockFile
     *
     * @return queryMockFile
     */
    public static String queryLocalFile(String fileName) {
        try {
            ClassPathResource classPathResource = new ClassPathResource(fileName);
            InputStream inputStream = classPathResource.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] flash = new byte[1024 * 10];
            int len = -1;
            while ((len = inputStream.read(flash)) != -1) {
                String str = new String(flash, 0, len);
                sb.append(str);
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String queryDefaultNodeStyleTemplate() {
        return queryLocalFile(DEFAULT_NODE_STYLE_TEMPLATE);
    }
    public static String queryDefaultFreeNodeStyleTemplate() {
        return queryLocalFile(DEFAULT_FREE_NODE_STYLE_TEMPLATE);
    }

    public static String queryDefaultNodeVueTemplate() {
        return queryLocalFile(DEFAULT_NODE_VUE_TEMPLATE);
    }

    public static String queryDefaultNodeVue() {
        return queryLocalFile(DEFAULT_NODE_VUE);
    }

    public static String queryDefaultEdgeStyleTemplate() {
        return queryLocalFile(DEFAULT_NODE_EDGE_TEMPLATE);
    }

    public static UnitViewNode buildDefaultNode(String name) {
        String nodeStyleTemplate = queryDefaultFreeNodeStyleTemplate();
        StringSubstitutor sub = new StringSubstitutor(buildNodeMap(name));
        nodeStyleTemplate = sub.replace(nodeStyleTemplate);
        UnitViewNode retNode = JSONUtil.toBean(nodeStyleTemplate, UnitViewNode.class);
        retNode.setNodeDataMode(NodeDataModeEnum.ALL.getCode());
        retNode.setNodeDataKey("default");
        retNode.setNodeType(NodeDataModeEnum.ALL.getCode());
        retNode.setNodeVueCode(queryDefaultNodeVue());
        retNode.setNodeVueType(NodeVueTypeEnum.DIALOG.getCode());
        return retNode;
    }

    private static Map<String, String> buildNodeMap(String name) {
        HashMap<String, String> nodeMap = Maps.newHashMap();
        nodeMap.put("nodeId", UUID.randomUUID().toString());
        nodeMap.put("nodeName", name);
        return nodeMap;
    }
}
